import sys
from oauth2client import client
from apiclient import sample_tools
import wx
import pprint
import shlex
class Gogo(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="Go Go Goo!", size=(400, 110))
        
        self.panel = wx.Panel(self) 
        
        #boxes
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        
        quote = wx.StaticText(self.panel, label="URL to shorten: ")
        hbox1.Add(quote, flag=wx.RIGHT, border=8)
        self.urltoshort = wx.TextCtrl(self.panel)
        hbox1.Add(self.urltoshort, proportion=1)
        vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        vbox.Add((-1, 10))
        
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        #button = wx.Button(panel, label="Shorten")
        lblname = wx.StaticText(self.panel, label="New Goo.gl URL: ")
        hbox2.Add(lblname)
        self.shorturl = wx.TextCtrl(self.panel)
        hbox2.Add(self.shorturl, proportion=1)
        vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT | wx.TOP | wx.RIGHT, border=10)
        vbox.Add((-1,10))
        self.panel.SetSizer(vbox)
        
        
        toolbar = self.CreateToolBar()
        atool = toolbar.AddLabelTool(wx.ID_ANY, 'Short', wx.Bitmap('scissors.png'))
        etool = toolbar.AddLabelTool(wx.ID_ANY, 'About', wx.Bitmap('box.png'))
        qtool = toolbar.AddLabelTool(wx.ID_ANY, 'Quit', wx.Bitmap('exit.png'))
        toolbar.Realize()

        self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
        self.Bind(wx.EVT_TOOL, self.OnButton, atool)
        self.Bind(wx.EVT_TOOL, self.OnAbout, etool)
        
    def OnQuit(self, e):
        self.Close()
    def OnAbout(self, e):
    	wx.MessageBox("Developed by MauroEldritch @ Eldritch Software - http://www.eldritch.com.ar", "About", wx.OK | wx.ICON_INFORMATION)
    def OnButton(self, e):
        shortest = self.urltoshort.GetValue()
        argv = shlex.split(shortest) #Shlex helps me to convert string to sys.argv style
        service, flags = sample_tools.init(argv, 'urlshortener', 'v1', __doc__, __file__, scope='https://www.googleapis.com/auth/urlshortener')
	try:
    		urlshort = shortest
    		url = service.url()
	    	body = {'longUrl': urlshort }
    		resp = url.insert(body=body).execute()
    		short_url = resp['id']
    		self.shorturl.SetValue(short_url)
  	except client.AccessTokenRefreshError:
    		wx.MessageBox("There was an error trying to fulfill your request. Please, verify the URL provided, it should have the next format: http://www.eldritch.com.ar", "Error", wx.OK)
    	
#------------------------

app = wx.App(False)
frame = Gogo(None)
frame.Show()
app.MainLoop()